package vn.njai.thousand_english_word.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import vn.njai.thousand_english_word.R;
import vn.njai.thousand_english_word.Utils.DataManager;

import java.util.ArrayList;

/**
 * Created by VUONG on 12/14/2014.
 */
public class ProgressActivity extends ActionBarActivity implements DataManager.LoadingListener{
    private String TAG = "ProgressActivity";
    private Context mContext;
    private ArrayList<Float> mProgressList = new ArrayList<Float>();
    private ArrayList<ProgressBar> mProgressListUI = new ArrayList<ProgressBar>();
    @InjectView(R.id.toolbar) public Toolbar mToolbar;
    @InjectView(R.id.btnDetail) public ImageButton mDetailBtn;
    @InjectView(R.id.progressBar1) public ProgressBar mProgressBar1;
    @InjectView(R.id.progressBar2) public ProgressBar mProgressBar2;
    @InjectView(R.id.progressBar3) public ProgressBar mProgressBar3;
    @InjectView(R.id.progressBar4) public ProgressBar mProgressBar4;
    @InjectView(R.id.txtProgressLearned) public TextView mProgressLearnedText;
    @InjectView(R.id.txtProgressTwoTime) public TextView mProgressTwoTimeText;
    @InjectView(R.id.txtProgressOneTime) public TextView mProgressOneTimeText;
    @InjectView(R.id.txtProgressNever) public TextView mProgressNeverText;
    @InjectView(R.id.txtProgressWait) public TextView mWaitText;
    @InjectView(R.id.txtLearned) public TextView mLearnedText;
    @InjectView(R.id.txtTwoTime) public TextView mTwoTimeText;
    @InjectView(R.id.txtOneTime) public TextView mOneTimeText;
    @InjectView(R.id.txtNever) public TextView mNeverText;

    public static void start(Context context) {
        Intent intent = new Intent(context,ProgressActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);
        mContext = getApplicationContext();
        init();
    }

    public void init(){
        ButterKnife.inject(this);
        initActionBar();
        mDetailBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ProgressDetailActivity.start(mContext);
            }
        });
        DataManager.getInstance().setListener(this);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                DataManager.getInstance().getDetailData();
            }
        },1000);

    }

    private void initActionBar(){
        mToolbar.setTitle(getString(R.string.actionbar_title_progress));
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_back);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onProgress(int progress) {

    }

    @Override
    public void onLoadDetailComplete() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mLearnedText.setText(getString(R.string.word_type_learned) + "(" + DataManager.getInstance().mDetailData.mListLeaned.size() + " Từ )");
                mTwoTimeText.setText(getString(R.string.word_type_twotime) + "(" + DataManager.getInstance().mDetailData.mListTwoTime.size() + " Từ )");
                mOneTimeText.setText(getString(R.string.word_type_onetime) + "(" + DataManager.getInstance().mDetailData.mListOneTime.size() + " Từ )");
                mNeverText.setText(getString(R.string.word_type_never) + "(" + DataManager.getInstance().mDetailData.mListNever.size() + " Từ )");

                float learned, twoTime, oneTime, never;
                learned = (float) (DataManager.getInstance().mDetailData.mListLeaned.size() * 100 / DataManager.getInstance().mWordCount);
                twoTime = (float) (DataManager.getInstance().mDetailData.mListTwoTime.size() * 100 / DataManager.getInstance().mWordCount);
                oneTime = (float) (DataManager.getInstance().mDetailData.mListOneTime.size() * 100 / DataManager.getInstance().mWordCount);
                never = 100 - learned - twoTime - oneTime;

                mProgressList.add(learned);
                mProgressList.add(twoTime);
                mProgressList.add(oneTime);
                mProgressList.add(never);

                mProgressLearnedText.setText(learned + "%");
                mProgressTwoTimeText.setText(twoTime + "%");
                mProgressOneTimeText.setText(oneTime + "%");
                mProgressNeverText.setText(never + "%");

                mProgressListUI.add(mProgressBar1);
                mProgressListUI.add(mProgressBar2);
                mProgressListUI.add(mProgressBar3);
                mProgressListUI.add(mProgressBar4);
                for (int i = 0; i < mProgressList.size(); i++) {
                    mProgressListUI.get(i).setProgress(mProgressList.get(i).intValue());
                    int rotation = 0;
                    for (int j = 0; j < i; j++) {
                        rotation += mProgressList.get(j);
                    }
                    mProgressListUI.get(i).setRotation(rotation * 3.6f);
                    mProgressListUI.get(i).requestLayout();
                }

                mDetailBtn.setVisibility(View.VISIBLE);
                mProgressLearnedText.setVisibility(View.VISIBLE);
                mProgressTwoTimeText.setVisibility(View.VISIBLE);
                mProgressOneTimeText.setVisibility(View.VISIBLE);
                mProgressNeverText.setVisibility(View.VISIBLE);
                mWaitText.setVisibility(View.GONE);
            }
        });
    }
}
